// pages/vipmember/vipmember.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ishidden: true,
    currentPage: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.goodslist(that.data.currentPage);
    that.lll();
    that.randCommentsTap();

  },
  onShow: function() {
    var that = this;
    utils.setTabBarBadge();
    that.personalInfo();
    that.lll();
  },
  preventTouchMove() {},
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      console.log(res);
      that.setData({
        info: res.datas
      })
      if (that.data.info.is_vip == 0) {
        that.setData({
          ishidden: true
        })
      }
    })
  },
  //rand_comments 精彩评价
  randCommentsTap: function(e) {
    var that = this;
    utils.http("/api/v1/order/rand_comments", {}, function(res) {
      console.log(res);
      that.setData({
        rand: res.datas
      })
    })
  },
  lll: function(e) {
    var that = this;
    if (that.data.allListData != undefined) {
      var allListData = that.data.allListData;
      for (var i in allListData) {
        allListData[i].number = 0;
        for (var k in wx.getStorageSync("listPost")) {
          if (wx.getStorageSync("listPost")[k].id == allListData[i].id) {
            allListData[i].number = wx.getStorageSync("listPost")[k].number;
          }
        }
      }
      that.setData({
        allListData: allListData
      })
    }
  },
  //专享会员所有的商品列表
  goodslist: function(currentPage) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/goods/goods_list", {
      page: currentPage
    }, function(res) {
      utils.hideLoading();
      var allpost = that.data.allListData;
      if (allpost == undefined) {
        allpost = [];
      }
      for (var i in res.datas) {
        res.datas[i].number = 0;
        allpost.push(res.datas[i]);
      }
      if (res.datas.length < 10) {
        that.setData({
          tishi_title: '已加载全部',
        });
      } else {
        that.setData({
          tishi_title: '加载更多',
        });
      }
      that.setData({
        allListData: allpost,
        ready: res.datas.length == 0 ? 1 : 2,
      })
      that.lll();
    })

  },
  /**
   * 判断是不是会员
   * 购物车数量++
   * **/
  onPrevTap: function(e) {
    var that = this;
    that.setData({
      ishidden: false
    })
  },
  //关闭
  onColse1Tap: function(e) {
    var that = this;
    that.setData({
      ishidden: true
    })
  },
  //授权登陆开通会员页面
  bindGetUserInfo: function(e) {
    var that = this;
    if (e.detail.errMsg == "getUserInfo:ok") {
      var data = {
        nickname: e.detail.userInfo.nickName,
        sex: e.detail.userInfo.gender,
        avatar: e.detail.userInfo.avatarUrl
      }
      app.memberModify(data)
      wx.navigateTo({
        url: '../openvip/openvip?viptype=' + e.currentTarget.dataset.viptype
      })
    }
  },
  //判断是不是会员   如果不是弹窗提示
  onOpenVipTap: function(e) {
    wx.navigateTo({
      url: '../openvip/openvip?viptype=' + e.currentTarget.dataset.viptype
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.ready == 2) {
      var page = that.data.currentPage + 1
      that.setData({
        currentPage: page,
      })
      that.goodslist(page)
    }

  },
  //加数量
  onPrevTap: function(e) {
    var that = this;
    var itemslist = that.data.allListData;
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.number;
    itemslist[index].number = itemslist[index].number + 1;
    that.setData({
      allListData: itemslist
    })
    var shuzi = wx.getStorageSync("number") || 0;
    var number = shuzi + 1
    that.setData({
      number: number
    })
    wx.setStorageSync("number", number);
    // //微信小程序 tabbar 上面设置消息数目等
    if (wx.getStorageSync("number") != 0) {
      wx.setTabBarBadge({
        index: 3,
        text: String(wx.getStorageSync("number"))
      })
    }
    var dataInfo = {
      id: e.currentTarget.dataset.id,
      image: e.currentTarget.dataset.image,
      name: e.currentTarget.dataset.name,
      price: e.currentTarget.dataset.price,
      subtitle: e.currentTarget.dataset.subtitle,
      vip_price: e.currentTarget.dataset.vipprice,
      selectStatus: false, //购物车做删除时候用
      number: 1 //加减数量
    }
    utils.uniqueSearchRecord(dataInfo);
  },
  //减数量
  onNextTap: function(e) {
    var that = this;
    var itemslist = that.data.allListData;
    var index = e.currentTarget.dataset.index;
    var ind = e.currentTarget.dataset.ind;
    if (itemslist[index].number > 0) {
      itemslist[index].number = itemslist[index].number - 1;
      var nowStorage = wx.getStorageSync("listPost");
      for (var i in nowStorage) {
        if (nowStorage[i].id == itemslist[index].id) {
          nowStorage[i].number = nowStorage[i].number - 1;
          if (nowStorage[i].number == 0) {
            nowStorage.splice(i, 1);
          }
        }
      }
      that.setData({
        number: wx.getStorageSync("number") - 1,
        allListData: itemslist
      })
      wx.setStorageSync("listPost", nowStorage)
      wx.setStorageSync("number", that.data.number);
      // // //微信小程序 tabbar 上面设置消息数目等
      if (wx.getStorageSync("number") != 0) {
        wx.setTabBarBadge({
          index: 3,
          text: String(wx.getStorageSync("number"))
        })
      } else {
        wx.removeTabBarBadge({
          index: 3
        })
      }
    } else {
      utils.message("该宝贝宝贝不能减少哦~")
    }

  },
  //立即邀请
  /**
   * 用户点击右上角分享（index.js）
   */
  onShareAppMessage: function(ops) {
    var that = this;
    console.log(that.data.info)
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '向本邀你来做超级会员，享无限好礼',
      path: 'pages/openvip/openvip?shareid=' + that.data.info.id + "&viptype=" + that.data.info.is_vip,
      success: function(res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
        console.log(res);
      },
      fail: function(res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }

  },
  //商品详情
  noFrutisDetailsTap: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    if (that.data.info.is_vip == 0) {
      that.setData({
        ishidden: false
      })
    } else {
      wx.navigateTo({
        url: '../commoditydetails/commoditydetails?id=' + id,
      })
    }
  },

})